package com.zhongkai.tools;

import java.lang.reflect.InvocationTargetException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Enumeration;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.PageContext;

import org.apache.struts.Globals;
import org.apache.struts.taglib.TagUtils;
import org.apache.struts.util.MessageResources;

public class StringTool {
	private static PageContext pageContext = null;

	public static String getArrayString(Object[] o) {
		String result = "";
		if (o != null) {
			for (Object obj : o) {
				result = result + obj.toString() + ",";
			}
			result = result.substring(0, result.lastIndexOf(","));
		}
		return result;
	}
	
	public static String getAutoFileName(String oldfilename) {
		String newfilename = null;
		long time = System.currentTimeMillis();
		int i = (int) (Math.random() * 1000 + 1);
		long result = time * 1000 + i;
		newfilename = String.valueOf(result);
		int pos = oldfilename.lastIndexOf(".");
		String extName = oldfilename.substring(pos, oldfilename.length());
		newfilename = newfilename + extName;
		return newfilename;
	}

	public static String getMessageByKey(String key) {
		String message = null;
		try {
			if (pageContext.getSession().getAttribute("org.apache.struts.action.LOCALE") != null) {
				Object args[] = new Object[5];
				message = TagUtils.getInstance().message(pageContext, null,
						Globals.LOCALE_KEY, key, args);
			} else {
				MessageResources resources = TagUtils.getInstance()
						.retrieveMessageResources(pageContext, null, false);
				Object oo = pageContext.getRequest().getAttribute(
						"common_user_langid");
				String langid = null;
				if (oo != null && oo instanceof String
						&& oo.toString().matches("\\d+"))
					langid = oo.toString();
				else
					langid = "1";
			
				Locale userLocale = getLocale(langid);
				message = resources.getMessage(userLocale, key);				
			}
			if (message == null) {
				message = "";
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return message;
	}
	
	public static Locale getLocale(String langid) throws Exception
	{
		Locale userLocale = null;
		if ("1".equals(langid))
			userLocale = new Locale("zh", "CN");
		else if ("2".equals(langid))
			userLocale = new Locale("zh", "TW");
		else if ("3".equals(langid))
			userLocale = new Locale("en");
		else if ("4".equals(langid))
			userLocale = new Locale("ko");
		else if ("5".equals(langid))
			userLocale = new Locale("ja");
		else if ("6".equals(langid))
			userLocale = new Locale("ru");
		else if ("7".equals(langid))
			userLocale = new Locale("fr");
		else if ("8".equals(langid))
			userLocale = new Locale("pt", "PT");
		else if ("9".equals(langid))
			userLocale = new Locale("es");
		else if ("10".equals(langid))
			userLocale = new Locale("it");
		else if ("11".equals(langid))
			userLocale = new Locale("de");
		else if ("12".equals(langid))
			userLocale = new Locale("vi");
		else if ("13".equals(langid))
			userLocale = new Locale("ar");
		else if ("14".equals(langid))
			userLocale = new Locale("hi");
		return userLocale;
	}

	public static String createAutoDir() {
		Date now_date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String str_date = sdf.format(now_date);
		int len = str_date.length();
		String str_dd = "01";
		if (len == 8)
			str_dd = str_date.substring(6, 8);
		if (len > 6)
			str_date = str_date.substring(0, 6);
		String sys_separator = System.getProperty("file.separator");
		String str_file = str_date + sys_separator + str_dd + sys_separator;
		return str_file;
	}

	public static String MatchDataBaseFieldName(String fieldName,
			String tableName) {
		Pattern p = Pattern.compile("^[A-Za-z]+");
		Matcher m = p.matcher(fieldName);
		String startstr = "";
		if (m.find())
			startstr = m.group();
		p = Pattern.compile("[A-Za-z]");
		m = p.matcher(startstr);
		StringBuffer sb = new StringBuffer();
		while (m.find()) {
			m.appendReplacement(sb, m.group() + "[A-Za-z0-9]+_");
		}
		String s = sb.substring(0, sb.length() - 1);
		p = Pattern.compile(s);
		m = p.matcher(tableName);
		String tname = "";
		if (m.find()) {
			tname = m.group();
			fieldName = fieldName.replaceFirst("^[A-Za-z]+", tname);
		}
		return fieldName;
	}
	
	public static String encrypt(String password) throws Exception
	{
		if(password!=null)
		{
			password="$k*^%"+password+"#@LxJ";
			password=Md5Encrypt.md5(password);	
			password="45eer"+password+"mi39e";
			password=Md5Encrypt.md5(password);	
			password="3r34l"+password+"jfai*";
			password=Md5Encrypt.md5(password);
			return password==null?"":password;
		}
		else
			return "";
	}

	public static PageContext getPageContext() {
		return pageContext;
	}

	public static void setPageContext(PageContext pageContext) {
		StringTool.pageContext = pageContext;
	}
	
	public static String getUrlPara(HttpServletRequest request) throws Exception
	{
		StringBuffer UrlWhere = new StringBuffer("");// url中的条件
		for (Enumeration i =request.getParameterNames(); i
				.hasMoreElements();) {
			String name = (String) i.nextElement();
			String value = request.getParameter(name);
			name = URLEncoder.encode(name, "UTF-8");
			value = URLEncoder.encode(value, "UTF-8");			
			UrlWhere.append("&").append(name).append("=").append(value);			
		}
		
		if(UrlWhere.length()>0)
			return UrlWhere.substring(1).toString();		
		else			
			return "";		
	}
	
	/*
	 * 分割时间字符串,并根据n来获取对应的数字数目
	 */
	public static String fengeDate(String date,int n){//2011-1-10
		String[] params=date.split("-");
		String dateString="";
		for(int i=0;i<n;i++)
		dateString+=params[i];
		return dateString;		
	}
	
	/*
	 * 时间字符串格式转换
	 */
	public  static String dateForString(Date date){
		SimpleDateFormat sqldateFormat = new SimpleDateFormat("yyyy-MM-dd");;
		return new String(sqldateFormat.format(date));//对日期进行格式化转换
	}
	
	
}
